﻿using InnoWeb.CEND.BL.Service;
using InnoWeb.CEND.Core.Model.General;
using InnoWeb.CEND.Data;
using InnoWeb.CEND.Data.Infrastructure;
using InnoWeb.CEND.Web.Framework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    public class IndustryPageController : Controller
    {
        private static WebSiteContext _context;
        private static IUnitOfWork _unitOfWork;

        private static GenericRepository<IndustryPageContent> _industryPageRepo;
        private static IndustryPageContentService _industryService;

        public IndustryPageController()
        {
            _context = new WebSiteContext(ConfigurationManager.ConnectionStrings["CendDBConnection"].ConnectionString);
            _unitOfWork = new UnitOfWork(_context);
            _context.Configuration.ValidateOnSaveEnabled = false;

            _industryPageRepo = new GenericRepository<IndustryPageContent>(_context);
            _industryService = new IndustryPageContentService(_unitOfWork, _industryPageRepo);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Details(int id)
        {
            return View();
        }

        #region Industry Page Creates

        public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        } 
        #endregion

        #region Industry Page Edits

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditHealthCare(int id)
        {
            var item = _industryService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditHealthCare(int id, IndustryPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                IndustryPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength >0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.HealthCareImage =  fileName + ".jpg";
                }
                else
                {
                    item.HealthCareImage = _industryService.GetElementById(model.Id).HealthCareImage;
                }

                _industryService.UpdateElement(item);
                _industryService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditBiotech(int id)
        {
            var item = _industryService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditBiotech(int id, IndustryPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                IndustryPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength >0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.BiotechImage =  fileName + ".jpg";
                }
                else
                {
                    item.BiotechImage = _industryService.GetElementById(model.Id).BiotechImage;
                }

                _industryService.UpdateElement(item);
                _industryService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditMedicalDevice(int id)
        {
            var item = _industryService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditMedicalDevice(int id, IndustryPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                IndustryPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength >0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.MedicalDeviceImage =  fileName + ".jpg";
                }
                else
                {
                    item.MedicalDeviceImage = _industryService.GetElementById(model.Id).MedicalDeviceImage;
                }

                _industryService.UpdateElement(item);
                _industryService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditIndustryExpertise(int id)
        {
            var item = _industryService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditIndustryExpertise(int id, IndustryPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                IndustryPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.IndustryExpertiseImage = fileName + ".jpg";
                }
                else
                {
                    item.IndustryExpertiseImage = _industryService.GetElementById(model.Id).IndustryExpertiseImage;
                }

                _industryService.UpdateElement(item);
                _industryService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        } 


        #endregion

        #region Industry Page Deletes

        public ActionResult Delete(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        } 
        #endregion
    }
}
